package com.wenhai.spring.questions;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * Spring 循环依赖 示例
 *
 * @author wenhai
 * @date   2021/4/13
 */
public class CircularReferenceDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context  = new AnnotationConfigApplicationContext();
        context.register(CircularReferenceDemo.class);
        context.getBeanFactory().addBeanPostProcessor(new MySmartInstantiationAwareBeanPostProcessor());
        context.refresh();
        Student student = context.getBean(Student.class);
        System.out.println(student);
        ClassRoot classRoot = context.getBean(ClassRoot.class);
        System.out.println(classRoot);
        context.close();
    }


    @Bean
    public static Student student() {
        Student student = new Student();
        student.setId(1L);
        student.setName("文海");
        return student;
    }
    @Bean
    public static ClassRoot classRoot() {
        ClassRoot classRoot = new ClassRoot();
        classRoot.setName("大一");
        return classRoot;
    }
}
